package com.nondo.dean.dealer.controller.wap.app;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.nondo.dean.common.web.View;
import com.nondo.dean.dealer.model.result.ActivityDetailResult;
import com.nondo.dean.dealer.service.ActivityService;

@Controller("apiWapActivity")
@RequestMapping("/wap/app/activity")
public class ActivityController {

    @Value("${baseUrl}")
    private String baseUrl;

    @SuppressWarnings("rawtypes")
    @Autowired
    private RedisTemplate redisTemplate;
    
    @Autowired
    private ActivityService  activityService;
    

    /**
     * 获取活动详情
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/info/{id}")
    public View<ActivityDetailResult> getActivityDetail(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View<ActivityDetailResult> view = new View<ActivityDetailResult>(20000, "查看成功");
        ActivityDetailResult beans=activityService.activityDetail(id, request);
        if(beans == null){
            beans = new ActivityDetailResult();
        }
        view.setData(beans);
        
        return view;
    }
    
    /**
     * 活动预览
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/view")
    public ModelAndView previewActivity(HttpServletRequest request, HttpServletResponse response, String id,
        Integer text) {
        ModelAndView mv = new ModelAndView("wap/app/activity/preview");
        if (text == 0) {
            @SuppressWarnings("unchecked")
            ValueOperations<String, ActivityDetailResult> valueOperations = redisTemplate.opsForValue();
            ActivityDetailResult beans = valueOperations.get(id);
            mv.addObject("contents", JSON.toJSONString(beans));
        }
        mv.addObject("baseUrl", baseUrl);
        return mv;
    }

}